package com.prj.ufdm.auth.bpo._impl;

import com.prj.ufdm.auth.bpo.SysFunctionBPO;
import com.prj.ufdm.auth.dao.SysFunctionDao;
import com.prj.ufdm.auth.dao.SysRoleFunctionDao;
import com.prj.ufdm.auth.model.SysFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import com.prj.ufdm.core.bpo._impl.BaseBPOImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统功能管理
 */
@Service 
public class SysFunctionBPOImpl extends BaseBPOImpl<SysFunctionDao,SysFunction> implements SysFunctionBPO{
	
    @Autowired
    private SysFunctionDao sysFunctionDao;
    
    @Autowired
    private SysRoleFunctionDao sysRoleFunctionDao;
    
    /**
	 * 描述: 查询所有功能信息
	 * @auther 胡义振
	 * @param userId 用户ID
	 * @return 用户资源列表
	 * @throws Exception
	 */
    @Override
	public List<SysFunction> doGetFunctionList() throws Exception
	{
		return sysFunctionDao.selectList(null);
	}
	
	
    /**
	 * 描述: 根据用户ID，查询用户的功能
	 * @auther 胡义振
	 * @param userId 用户ID
	 * @return 用户资源列表
	 * @throws Exception
	 */
    @Override
	public List<SysFunction> doGetFunctionListByUserId(String userId) throws Exception
	{
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId", userId);
		return sysFunctionDao.selectFunctionListByUserId(map);
	}
	
    /**
	 * 描述: 根据用户ID，查询用户授权功能
	 * @auther 胡义振
	 * @param userId 用户ID
	 * @return 用户资源列表
	 * @throws Exception
	 */
    @Override
	public Collection<GrantedAuthority> doGetGantedAuthoritiestByUserId(String userId) throws Exception{
		List<SysFunction> sysFunctionList = doGetFunctionListByUserId(userId);
		Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		for (SysFunction sysFunction : sysFunctionList) {
			if (sysFunction != null && sysFunction.getFunctionUrl() != null) {
				// 此处将权限信息添加到 GrantedAuthority 对象中，在后面进行全权限验证时会使用 GrantedAuthority 对象
				if(sysFunction.getFunctionUrl()==null || sysFunction.getFunctionUrl().equals("")) {
					 grantedAuthorities.add(new SimpleGrantedAuthority(sysFunction.getId())); 
				}else {
				    grantedAuthorities.add(new SimpleGrantedAuthority(sysFunction.getFunctionUrl())); 
				}
			}
		}
		return grantedAuthorities;
	}
	
	
	/**
	 * 描述: 根据角色ID，查询角色的功能
	 * @auther 胡义振
	 * @param roleId 角色ID
	 * @return 
	 * @throws Exception
	 */
    @Override
	public List<SysFunction> doGetFunctionListByRoleId(String roleId) throws Exception
	{
		Map<String,String> map = new HashMap<String,String>();
		map.put("roleId", roleId);
		return sysFunctionDao.selectFunctionListByRoleId(map);
	}
	
    
	/**
	 * 描述: 删除系统功能
	 * @auther 胡义振
	 * @param functionId 系统功能ID
	 * @return 
	 * @throws Exception
	 */
    @Override
	public Integer doRmFunctionById(String functionIds) throws Exception
	{
		if (functionIds == null)  functionIds = "";
		String[] arrFunctionId = functionIds.split(",");
		for(String functionId : arrFunctionId){
			// 删除角色-功能表中的对应关系
			sysRoleFunctionDao.deleteRoleFunctionByFunctionId(functionId);
			// 删除功能表
			sysFunctionDao.deleteById(functionId);
		}
		return 0;
	}

	
}
